$(document).ready( function() {

	var allCheck = $(".data #allCheck");// 全选复选框
	var keysCheck = $(".data input[name='key']");// ID复选框
	var deleteButton = $(".panel .btn_del");// 删除按钮
	
	var listForm = $("form.listForm");// 列表表单
	var searchButton =  $(".search .btn_search");// 查询按钮
	var pagenum = $(".panel input.pagenum");// 当前页码
	
	var selectNum = $(".panel select.selectNum");//页面下拉框
	
	// 全选
	allCheck.click(function() {
		if ($(this).attr("checked")) {
			keysCheck.attr("checked", true);
			deleteButton.attr("disabled", false);
		} else {
			keysCheck.attr("checked", false);
			deleteButton.attr("disabled", true);
		}
	});
	
	// 无复选框被选中时,删除按钮不可用
	keysCheck.click( function() {
		var keysChecked = $(".data input[name='key']:checked");
		if (keysChecked.size() > 0) {
			deleteButton.attr("disabled", false);
		} else {
			keysCheck.attr("checked", false);
			deleteButton.attr("disabled", true);
		}
		allCheck.attr("checked", keysCheck.size()==keysChecked.size());
	});
	
	// 批量删除
	deleteButton.click( function() {
		var btn = this;
		$.conFirm("删除确认","确定删除?",function(){
			del(btn);
		});
		
	});
	
	// 查找
	searchButton.click( function() {
		pagenum.val("1");
		listForm.submit();
	});
	
	function del(btn){
		var url = $(btn).attr("url");
		var keysCheckedCheck = $(".data input[name='key']:checked");
		$.ajax({
			url: url,
			data: keysCheckedCheck.serialize(),
			dataType: "json",
			async: false,
			beforeSend: function(data) {
				deleteButton.attr("disabled", true);
			},
			success: function(data) {
				deleteButton.attr("disabled", false);
				if (data.success) {
					keysCheckedCheck.parent().parent().remove();
					//提示删除成功
					$.tipTool("删除成功!");
					listForm.submit();
				}
				else{
					deleteButton.attr("disabled", false);
					$.tipTool("删除失败:"+data.resultMsg);
				}
			}
		});
	}
	
	
});